//
//  ZLW_DBManager.m
//  EPA
//
//  Created by lanou3g on 16/4/26.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ZLW_DBManager.h"
#import "FMDB.h"
#import "ZLWUserInfoManager.h"
@implementation ZLW_DBManager


+ (instancetype)shareMesager
{
    static ZLW_DBManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ZLW_DBManager alloc] init];
    });
    return manager;
}

static FMDatabase *database = nil;
- (void)openDB
{
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    //数据库路径
    NSString *dbPath = [documentPath stringByAppendingPathComponent:@"PK.sqlite"];
    NSLog(@"%@",dbPath);
    //打开,创建数据库
    database = [FMDatabase databaseWithPath:dbPath];
    if ([database open]) {
        NSLog(@"数据库打开成功");
    }
}

/**
 *  创建表
 */
-(void)createTable
{
    NSString *sql = @"CREATE TABLE  IF NOT EXISTS shoucang (collectid INTEGER PRIMARY KEY AUTOINCREMENT ,uid TEXT ,contentid TEXT ,title TEXT , content TEXT ,coverimg TEXT ,uname TEXT ,html TEXT) ";
    BOOL result = [database executeUpdate:sql];
    if (result) {
        NSLog(@"创建表格成功");
    }
}

- (void)insertModel: (ZLWHomeListModel *)model html :(NSString *)html
{
    BOOL resilt = [database executeUpdateWithFormat:@"INSERT INTO shoucang (uid ,contentid ,title, content, coverimg ,uname,html) VALUES (%@,%@,%@,%@,%@,%@,%@)",[ZLWUserInfoManager shareManager].uid ,model.ID,model.title,model.content,model.coverimg,model.name,html];
    if (resilt ) {
        NSLog(@"______________________________________________________________________插入数据成功");
    }
    
}


/**
 *  收藏
 */
- (void)collectDetailModel : (ZLWHomeListModel *)dModel html:(NSString *)htmlStr
{
    [self openDB];
    [self createTable];
    [self insertModel:dModel html:htmlStr];
}
/**
 *  取消 收藏
 */
- (void)cancelCollectArticle: (NSString *)contentID
{
    [self openDB];
    [self createTable];
    BOOL result = [database executeUpdateWithFormat:@"DELETE FROM shoucang WHERE contentid = %@",contentID];
    if (result) {
        NSLog(@"删除成功");
    }
}

/**
 *  查询所有收藏的内容
 */
- (NSMutableArray *)SelectAllCollect
{
    [self openDB];
    [self createTable];
    FMResultSet *resultSet = [database executeQueryWithFormat:@"SELECT * FROM shoucang WHERE uid = %@",[ZLWUserInfoManager shareManager].uid];
    //声明一维数组
    NSMutableArray *array = [NSMutableArray new];
    while ([resultSet next]) {
        ZLWHomeListModel *model = [ZLWHomeListModel new];
        //赋值
        model.content = [resultSet stringForColumn:@"content"];
        model.coverimg = [resultSet stringForColumn:@"coverimg"];
        model.ID = [resultSet stringForColumn:@"contentid"];
        model.title = [resultSet stringForColumn:@"title"];
        model.name = [resultSet stringForColumn:@"uname"];
        model.htmlString   = [resultSet stringForColumn:@"html"];
        //添加到数组
        [array addObject:model];
    }
    return array;
}
/**
 *  判断是否收藏
 */
- (BOOL)HasCollect: (NSString *)contentID
{
    BOOL hasCollect = NO;
    [self openDB];
    [self createTable];
    
    FMResultSet *result = [database executeQueryWithFormat:@"SELECT * FROM shoucang WHERE uid = %@ AND contentid = %@",[ZLWUserInfoManager shareManager].uid, contentID];
    while ([result next]) {
        hasCollect = YES;
    }
    return  hasCollect;
}

@end
